<?php

class Tiger_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{

    /**
     * This function is called once after router shutdown. It automatically
     * sets the layout for the default MVC or a module-specific layout. If
     * you need to set a custom layout based on the controller called, you
     * can set it here using a switch based on the action or controller or
     * set the layout in the controller itself.
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
    	$module     = strtolower($request->getModuleName());
        $controller = strtolower($request->getControllerName());
        $action     = strtolower($request->getActionName());

        if ($module !== 'default') {
        	$layout = Zend_Layout::getMvcInstance();
            $layout->setLayoutPath(APPLICATION_PATH . '/modules/'
                . $module . '/layouts/scripts/');
            $layout->setLayout($module);
        }
        
    	/*if ($module !== 'default') {
    	  	$layout = Zend_Layout::getMvcInstance();
            $layout->setLayoutPath(APPLICATION_PATH . '/layouts/scripts/');
            $layout->setLayout($module);
        }*/
        
        $this->_checkAdminPermit($request);
    }
    
	private function _checkAdminPermit(Zend_Controller_Request_Abstract $request)
    {    	
    	$params = $request->getParams();
   		$module = $params["module"];
        $controller = $params["controller"];
        $action = $params["action"];           
        
        //action必须判断，否则会进入派遣死循环。
        if (($module == 'admin' ) && ($action != 'login') && ($action != 'cheklogin') && ($action != 'ajaxcheklogin'))
        {         
            $authNamespace = new Zend_Session_Namespace('HoldBB_Auth');
            //管理员未登录
            //echo "用户名：" . $authNamespace->user;
            if(! isset($authNamespace->user))
            {
            	$redirector = new Zend_Controller_Action_Helper_Redirector(); 
                //跳到管理员登录页面 
                $redirector->direct('login', 'index', 'admin');
            }	else {//已经登陆，根据角色验证访问权限
            	
            }
        }
    }

}